package com.iotechn.unimall.admin.api.biaozhun;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopBiaozhunDO;
import com.iotechn.unimall.data.domain.ShopBiaozhunLogDO;
import com.iotechn.unimall.data.mapper.ShopBiaozhunLogMapper;
import com.iotechn.unimall.data.mapper.ShopBiaozhunMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminBiaozhunLogServiceImpl implements AdminBiaozhunLogService {
    @Autowired
    private ShopBiaozhunLogMapper shopBiaozhunMapper;
    @Autowired

    private ShopBiaozhunMapper shopBiaozhun1Mapper;


    @Override
    public Page<ShopBiaozhunLogDO> list(Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopBiaozhunLogDO> wrapper = new EntityWrapper<ShopBiaozhunLogDO>();
       
        wrapper.orderBy("id", false);
        Integer count = shopBiaozhunMapper.selectCount(wrapper);
        List<ShopBiaozhunLogDO> ShopBiaozhunLogDOS = shopBiaozhunMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(ShopBiaozhunLogDOS, page, limit, count);
    }
    @Override
    public List<ShopBiaozhunLogDO> getBiaozhunByShopId(Long shopId,Long jiedianId) throws ServiceException {

         List<ShopBiaozhunLogDO> ShopBiaozhunLogDOS = shopBiaozhunMapper.getBiaozhunByShopId(shopId,jiedianId);
         if(ShopBiaozhunLogDOS==null || ShopBiaozhunLogDOS.size()==0){
             List<ShopBiaozhunDO> shopBiaozhunLogDOS= shopBiaozhun1Mapper.getBiaozhunByShopId(shopId,jiedianId);
         }
         return ShopBiaozhunLogDOS;
    }
    @Override
    public List<ShopBiaozhunLogDO> getBiaozhunByLogId(Long logId) throws ServiceException {


        Wrapper<ShopBiaozhunLogDO> wrapper = new EntityWrapper<ShopBiaozhunLogDO>();
        wrapper.eq("shop_log_id",logId);
        List<ShopBiaozhunLogDO> ShopBiaozhunLogDOS = shopBiaozhunMapper.selectList(wrapper);
        return ShopBiaozhunLogDOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopBiaozhunLogDO create(ShopBiaozhunLogDO ShopBiaozhunLogDO, Long adminId) throws ServiceException {
        ShopBiaozhunLogDO.setAdminId(adminId);
        Date now = new Date();
        ShopBiaozhunLogDO.setGmtCreate(now);
        ShopBiaozhunLogDO.setGmtUpdate(now);
        if(ShopBiaozhunLogDO.getImgsList()!=null){
            Gson gson=new Gson();
            ShopBiaozhunLogDO.setFile(gson.toJson(ShopBiaozhunLogDO.getImgsList()));
        }
        if (shopBiaozhunMapper.insert(ShopBiaozhunLogDO) > 0) {
            ShopBiaozhunLogDO.setId(ShopBiaozhunLogDO.getId());
            return ShopBiaozhunLogDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopBiaozhunLogDO ShopBiaozhunLogDO, Long adminId) throws ServiceException {
        Long id = ShopBiaozhunLogDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(ShopBiaozhunLogDO.getImgsList()!=null && ShopBiaozhunLogDO.getImgsList().size()>0){
            Gson gson=new Gson();
            ShopBiaozhunLogDO.setFile(gson.toJson(ShopBiaozhunLogDO.getImgsList()));
        }
        ShopBiaozhunLogDO.setGmtUpdate(now);
        if (shopBiaozhunMapper.updateById(ShopBiaozhunLogDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopBiaozhunMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopBiaozhunMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopBiaozhunLogDO detail(Long id, Long adminId) throws ServiceException {
        return shopBiaozhunMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopBiaozhunLogDO ShopBiaozhunLogDO = new ShopBiaozhunLogDO();
        ShopBiaozhunLogDO.setId(id);
        ShopBiaozhunLogDO.setGmtUpdate(new Date());
        if (shopBiaozhunMapper.updateById(ShopBiaozhunLogDO) > 0) {
            return true;
        }
        return false;
    }

}